﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.ComponentModel;
using Skynet.BusinessEntities;
using System.Web.Security;
using System.Web;
using Skynet.BusinessLogic.BusinessComponents;
namespace Skynet.WebAdministracion.Views.EvaluationMgm
{
    [System.ComponentModel.DataObject]
    public class EvaluationMgmPresenter : Infrastructure.Web.Views.Master.MasterPresenter<IEvaluationMgmView>
    {
        [Dependency]
        public WebFoundation.Services.Session.ISessionService SessionService { get; set; }


        [Dependency]
        public WebFoundation.Services.Navigation.INavigationService NavigationService { get; set; }


        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }
        [Dependency]
        public BusinessLogic.BusinessComponents.IBCCustomers BCCustomers { get; set; }
        [Dependency]
        public IBCCustomersQueries BCCustomersQueries { get; set; }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public object GetActiveExams(Guid ExamByCustomerId)
        {
            if (ExamByCustomerId == Guid.Empty)
            {
                return null;
            }
            var exam = this.BCExamQueries.SearchExamByCustomer(options: BusinessLogic.BusinessComponents.ExamByCustomerSearchOptions.RuntimeByCustomerExamId, ExamByCustomerId: ExamByCustomerId).SingleOrDefault();
            var query = new { Id = exam.Id, Category = exam.Exam.ExamCategory.Title, Title = exam.Exam.Title, Duration = exam.Duration, ValidFrom = exam.ValidFrom, ValidTo = exam.ValidTo };
            return query;
        }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public IEnumerable<object> GetActiveExams()
        {
            var userName = HttpContext.Current.User.Identity.Name;
            var customer = this.BCCustomersQueries.FindCustomers(option: BusinessLogic.BusinessComponents.CustomerFindOptions.GetByUserName, UserName: userName).SingleOrDefault();
            var exams = this.BCExamQueries.SearchExamByCustomer( options: ExamByCustomerSearchOptions.RuntimeActiveByCustomerId,  CustomerId:customer.Id);
            var query = (from c in exams select new { Id = c.Id, Category = c.Exam.ExamCategory.Title, Title = c.Exam.Title, Duration = c.Duration }).ToList(); 
            return query;
        }
        public void AcceptExam(Guid key)
        {
            this.SessionService.ExamByCustomerId = key;
            this.NavigationService.NavigateRoute(WebFoundation.Constants.RoutesNames.RuntimeExamStart);
            
        }
    }
}
